/** 
 * Project Name:eleme-website 
 * File Name:ShopCartServiceImpl.java 
 * Package Name:com.eleme.service 
 * Date:2017年12月28日下午2:59:03 
 * Copyright (c) 2017, fakedesigner@163.com All Rights Reserved. 
 * 
*/  
  
package com.eleme.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.eleme.dao.ICartMemberDao;
import com.eleme.dao.IDaoFactory;
import com.eleme.dao.IShopCartDao;
import com.eleme.entity.ShopCart;
import com.eleme.entity.ShopCartDtl;
import com.eleme.service.IShopCartService;

/** 
 * ClassName:ShopCartServiceImpl <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason:   TODO ADD REASON. <br/> 
 * Date:     2017年12月28日 下午2:59:03 <br/> 
 * @author   Pan 
 * @version   
 * @since    JDK 1.8 
 * @see       
 */
public class ShopCartServiceImpl extends BaseServiceImpl<ShopCart> implements IShopCartService {

	private IShopCartDao dao;
	private ICartMemberDao dao2;
	
	public ShopCartServiceImpl(IDaoFactory factory) {
		super(factory.getShopCartDao());
		dao = (IShopCartDao) super.getDao();
		dao2 = factory.getCartMemberDao();
	}
	





	public boolean saveShopCart(String cookie, int memberid) {
		List<ShopCart> li = new ArrayList<ShopCart>();
		ShopCart sc = cookieToShopCart(cookie);
		sc.setMemberid(memberid);
		li.add(sc);
		dao.clearShopCart(memberid);
		dao.saveShopCart(li);
		return false;
	}






	public ShopCart readShopCart(int memberid) {
		return dao.readShopCart(memberid).get(0);
	}






	public String readShopCartCookie(int memberid) {
		return shopCartToCookie(dao.readShopCart(memberid).get(0));
	}


	protected String shopCartToCookie(ShopCart sc) {
		StringBuilder sb = new StringBuilder();
		sb.append(sc.getShopid() + ":");
		for(ShopCartDtl dtl : sc.getLstDtl()) {
			sb.append(dtl.getGoodsid() + "," + dtl.getQty() + "," + dtl.getTotal() + "|");
		}
		return sb.toString();
	}

	protected ShopCart cookieToShopCart(String cookie) {
		if (cookie != null) {
			int shopid = new Integer(cookie.substring(0, cookie.indexOf(':'))).intValue();
			int cnt = 0;
			cookie = cookie.substring(cookie.indexOf(':')+1, cookie.length());
			ShopCart mst = new ShopCart();
			mst.setCartno(1);
			mst.setShopid(shopid);
			mst.setLstDtl(new ArrayList<ShopCartDtl>());
			String[] dtls = cookie.split("\\|");
			int i = 0;
			for(String dtl : dtls) {
				String[] arrdtl = dtl.split(",");
				ShopCartDtl ch = new ShopCartDtl();
				ch.setCartno(1);
				ch.setGoodsid(new Integer(arrdtl[0]).intValue());
				ch.setQty(new Integer(arrdtl[1]).intValue());
				ch.setTotal(new Float(arrdtl[2]).floatValue());
				ch.setPrice(ch.getTotal()/ch.getQty());
				ch.setSpec(0);
				ch.setLine(++i);
				mst.getLstDtl().add(ch);
				cnt++;
			}
			mst.setDtlcnt(cnt);
			return mst;
		}
		return null;
	}

	protected int getMemberidByName(String username) {
		return dao2.getMemberidByName(username);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
